const Service = require('egg').Service;

class Top250Service extends Service {
  async find({start,limit}) {    //查询
    // var top250 = await this.ctx.model.Top250.find({})
    var data = await this.ctx.model.Top250.find({}).skip(Number(start)).limit(Number(limit));
    var total = await this.ctx.model.Top250.find({}).count();
    var arr = [];
        for (var i = 0; i < total; i += 5) {
            arr.push(i)
        }
        var res = arr.map(item => {
            return (item + 5) / 5
        })
        // console.log(res)//12345
        var min = Math.min(...res)
        var max = Math.max(...res)
        if (start == undefined || start == 1) {
            start = 0
        }
        if (start <= min) {
            start = min
        }
        if (start >= max) {
            start = max
        }
        if (limit == undefined) {
            limit = 5
        } //1,2,3,4
        start = (start - 1) * 5//[0,5,10,15,20]
        var data = await this.ctx.model.Top250.find().skip(Number(start)).limit(Number(limit));
        var next = start / 5 + 2
        var previous = start / 5

    return {data,total,next, previous, res};
  }
  async updateOne(id) {    //点赞
    // var top250 = await this.ctx.model.Top250.find({})
    var res = await this.ctx.model.Top250.updateOne({_id:id},{collected:true});
    // console.log(res)
    return res;
  }
  async updateTwo(id) {    //取消点赞
    // var top250 = await this.ctx.model.Top250.find({})
    var res = await this.ctx.model.Top250.updateOne({_id:id},{collected:false});
    // console.log(res)
    return res;
  }
  async deleteOne(id) {    //删除
    // var top250 = await this.ctx.model.Top250.find({})
    var res = await this.ctx.model.Top250.deleteOne({_id:id});
    // console.log(res)
    return res;
  }
  async findDetail(id) {    //详情
    // var top250 = await this.ctx.model.Top250.find({})
    var res = await this.ctx.model.Top250.find({_id:id});
    // console.log(res)
    return res;
  }
  
  //累加增删改查
}
module.exports = Top250Service;
